import http from "./http"
import ut from "@/assets/utils"
import store from "@/store"

// 获取用户状态【是否禁用】
const queryUserStatus = async () => {
	let cache = ut.storage.get('cache_user_isDisable')
	if (cache) return cache
	let arr = []
	try {
		let { code, data } = await http.get('/system/dict/data/type/sys_normal_disable')
		if (+code === 200) {
			arr = data.map(item => {
				return {
					dictLabel: item.dictLabel,
					dictValue: item.dictValue
				}
			})
			ut.storage.set('cache_user_isDisable', arr)
		}

	} catch (_) { }
	return arr
}

// 获取用户性别【男/女/未知】
const queryUserSex = async () => {
	let cache = ut.storage.get('cache_user_sex')
	if (cache) return cache
	let arr = []
	try {
		let { code, data } = await http.get('/system/dict/data/type/sys_user_sex')
		if (+code === 200) {
			arr = data.map(item => {
				return {
					dictLabel: item.dictLabel,
					dictSort: item.dictSort
				}
			})
			ut.storage.set('cache_user_sex', arr)
		}

	} catch (_) { }
	return arr
}

// 获取部门树【男/女/未知】
const queryDepTree = async () => http.get('/system/user/deptTree')


// 获取用户列表【支持分页/用户名称/手机号码/状态/创建时间】
const queryUserData = obj => {
	let { pageNum = 1, pageSize = 10, userName, phonenumber, status, dateRange, deptId } = obj,
		params = { pageNum, pageSize }

	if (userName) params.userName = userName
	if (phonenumber) params.phonenumber = phonenumber
	if (status) params.status = status
	if (dateRange.length > 0) {
		params['params[beginTime]'] = dateRange[0]
		params['params[endTime]'] = dateRange[1]
	}
	if (deptId) params.deptId = deptId
	return http.get('/system/user/list', { params })
}

// 用户状态更改
// /system/user/changeStatus
const updateUserStatus = obj => http.put('/system/user/changeStatus', obj)




export default {
	queryUserStatus,
	queryUserSex,
	queryDepTree,
	queryUserData,
	updateUserStatus
}

/* const dateRange = ["2022-06-08", "2022-06-08"];
const params = {
	beginTime: dateRange[0],
	endTime: dateRange[1]
};
params['params[beginTime]'] = dateRange[0]
		params['params[endTime]'] = dateRange[1]

const queryString = Object.entries(params).map(([key, value]) => `${encodeURIComponent(`params[${key}]`)}=${encodeURIComponent(value)}`).join('&');

console.log(queryString); */